/**************************************************************************
 *
 * Copyright (C) 2007 University of California, Los Angeles.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *************************************************************************/

package edu.ucla.cs.typecast.rmi.server;

import java.lang.reflect.Method;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Hashtable;

import edu.ucla.cs.typecast.discovery.ServiceDiscoveryConstants;
import edu.ucla.cs.typecast.discovery.ServiceDiscoveryServer;
import edu.ucla.cs.typecast.discovery.ServiceDiscoveryService;
import edu.ucla.cs.typecast.net.EndPoint;
import edu.ucla.cs.typecast.net.EndPointListener;
import edu.ucla.cs.typecast.net.EndPointListenerFactory;
import edu.ucla.cs.typecast.net.TransportContext;
import edu.ucla.cs.typecast.net.TypeCastConstants;
import edu.ucla.cs.typecast.rmi.MethodInvocation;

/**
 * 
 * This class manages all the TypeCast service objects on a node. It is a 
 * singleton. 
 * 
 * @date Jul 25, 2007
 *
 */
public class TypeCastObjectManager {
	private static TypeCastObjectManager instance = new TypeCastObjectManager(); 
	private Hashtable<EndPoint, TypeCastServiceRegistrar> registrars = new Hashtable<EndPoint, TypeCastServiceRegistrar>(); 
	private Hashtable<Class, Object> metaServiceMap = new Hashtable<Class, Object>(); 

	
	public static TypeCastObjectManager getInstance() { 
		return instance; 
	}
	
	private TypeCastObjectManager() { 
		metaServiceMap.put(ServiceDiscoveryService.class , new ServiceDiscoveryServer()); 
	}		

	public synchronized TypeCastServiceRegistrar getServiceRegistrar() { 
		try { 
			return this.getServiceRegistrar(new EndPoint(InetAddress.getByName(TypeCastConstants.DEFAULT_RMI_ADDRESS), TypeCastConstants.DEFAULT_RMI_PORT)); 
		} catch(Throwable t) { 
			throw new RuntimeException(t); 
		}
	}

	
	/**
	 * Get the TypeCastServiceRegistrar this is repsonsbile for a specific network end point. 
	 * @param endPoint
	 * @return
	 */
	public synchronized TypeCastServiceRegistrar getServiceRegistrar(EndPoint endPoint) { 
		TypeCastServiceRegistrar registrar = registrars.get(endPoint); 
		if (registrar == null) { 
			EndPointListener listener = EndPointListenerFactory.createEndPointListener(endPoint); 
			TypeCastMethodInvocationDispatcher dispatcher = new TypeCastMethodInvocationDispatcher(endPoint); 
			listener.setTypeCastDispatcher(dispatcher); 
			registrar = new TypeCastServiceRegistrar(endPoint, listener); 
			registrars.put(endPoint,registrar); 
		}
		return registrar; 
	}
	
	public synchronized TypeCastServiceRegistrar getServiceRegistrar(EndPoint endPoint, EndPointListener listener) { 
		TypeCastServiceRegistrar registrar = registrars.get(endPoint); 
		if (registrar == null) { 
			if (listener == null) { 
				listener = EndPointListenerFactory.createEndPointListener(endPoint);
			}
			TypeCastMethodInvocationDispatcher dispatcher = new TypeCastMethodInvocationDispatcher(endPoint); 
			listener.setTypeCastDispatcher(dispatcher); 
			registrar = new TypeCastServiceRegistrar(endPoint, listener); 
			registrars.put(endPoint,registrar); 
		}
		return registrar; 
	}
	
	public synchronized Collection<ServiceRegistrationEntry> getServiceRegistrationEntries(Class[] types) { 
		ArrayList<ServiceRegistrationEntry> result = new ArrayList<ServiceRegistrationEntry>(); 
		for(TypeCastServiceRegistrar registrar: registrars.values()) { 
			result.addAll(registrar.getServiceRegistrationEntries(types)); 
		}
		return result; 
	}
		
	public Object getMetaServiceObject(Class serviceType) { 
		Object metaService = metaServiceMap.get(serviceType); 
		if (metaService == null) { 
			throw new RuntimeException("No service object found to serve " + serviceType); 
		}
		return metaService; 
	}
}
